package Dao;

import Pojo.Bank;
import Utils.DBUtil;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class BankDao {
    private DBUtil dbUtil = new DBUtil();
    private Logger logger = Logger.getLogger(BankDao.class);

    /**
     * 获取所有银行数据
     *
     * @return
     * @throws SQLException
     */
    public List<Bank> getBankMsgAll() throws SQLException {
        String sql = "select " +
                "bankId," +
                "bankName " +
                "from `bank` ";
        logger.info("sql=>" + sql);
        return dbUtil.queryDatas(sql, null, Bank.class);
    }

    /**
     * 添加银行
     *
     * @param bankName
     * @return
     * @throws SQLException
     */
    public int addBankIsAdmin(String bankName) throws SQLException {
        String sql = "insert into `bank`(bankName) values(?)";
        Object[] params = {bankName};
        logger.info("sql=>" + sql);
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.update(sql, params);
    }

    /**
     * 修改银行名称
     *
     * @param bankName
     * @param bankId
     * @return
     * @throws SQLException
     */
    public int updateBankMsgIsAdminByBankId(String bankName, String bankId) throws SQLException {
        String sql = "update `bank` set " +
                "bankName = ? " +
                "where " +
                "bankId = ? ";
        Object[] params = {bankName, bankId};
        return dbUtil.update(sql, params);
    }

    public int deleteBankIsAdminByBankId(String bankId) throws SQLException {
        String sql = "delete from `bank` where bankId = ? ";
        Object[] params = {bankId};
        logger.info("sql=>" + sql);
        logger.info("params=>" + Arrays.toString(params));
        return dbUtil.update(sql, params);
    }
}
